package com.yiwei.ydd.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import com.yiwei.ydd.R;
import com.yiwei.ydd.util.DisplayUtil;

public class PreviewBorderView extends SurfaceView implements Callback, Runnable {
    private static final String DEFAULT_TIPS_TEXT = "请将方框对准证件拍摄";
    private static final int DEFAULT_TIPS_TEXT_COLOR = -1;
    private static final int DEFAULT_TIPS_TEXT_SIZE = 16;
    private Canvas mCanvas;
    private SurfaceHolder mHolder;
    private Paint mPaint;
    private Paint mPaintLine;
    private Paint mPaintText;
    private int mScreenH;
    private int mScreenW;
    private Thread mThread;
    private String tipText;
    private int tipTextColor;
    private float tipTextSize;
    private RectF ractf;


    public PreviewBorderView(Context context) {
        this(context, null);
    }

    public PreviewBorderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PreviewBorderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        init(context);
    }


    /**
     * 初始化自定义属性
     *
     * @param context Context
     * @param attrs   AttributeSet
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PreviewBorderView);
        try {
            tipTextSize = a.getDimension(R.styleable.PreviewBorderView_tipTextSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TIPS_TEXT_SIZE, getResources().getDisplayMetrics()));
            tipTextColor = a.getColor(R.styleable.PreviewBorderView_tipTextColor, DEFAULT_TIPS_TEXT_COLOR);
            tipText = a.getString(R.styleable.PreviewBorderView_tipText);
            if (tipText == null) {
                tipText = DEFAULT_TIPS_TEXT;
            }
        } finally {
            a.recycle();
        }


    }

    /**
     * 初始化绘图变量
     */
    private void init(Context paramContext) {
        this.mHolder = getHolder();
        this.mHolder.addCallback(this);

//        设置透明
        this.mHolder.setFormat(PixelFormat.TRANSPARENT);
//        控制窗口中表面的视图层是否放置在常规视图层的顶部。
        setZOrderOnTop(true);
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.mPaint.setColor(-1);
        this.mPaint.setStyle(Style.FILL_AND_STROKE);

//        清空图像
        this.mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        this.mPaintLine = new Paint();
        this.mPaintLine.setColor(ContextCompat.getColor(paramContext, R.color.colorAccent));
        this.mPaintLine.setStrokeWidth(10.0F);
        this.mPaintText = new Paint();
        this.mPaintText.setColor(-1);
        this.mPaintText.setStrokeWidth(5.0F);
        setKeepScreenOn(true);
    }

    /**
     * 绘制取景框
     */
    private void draw() {
        try {
            this.mCanvas = this.mHolder.lockCanvas();
            this.mCanvas.drawARGB(100, 0, 0, 0);
            this.mScreenW = ((int) (this.mScreenH * 1.8D - DisplayUtil.dip2px(getContext(), 10.0F)));
            Log.e("TAG", "mScreenW:" + this.mScreenW + " mScreenH:" + this.mScreenH);
            if(ractf==null){
                ractf = new RectF(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH - this.mScreenH / 6);
            }
            this.mCanvas.drawRect(ractf, this.mPaint);
            this.mCanvas.drawLine(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH / 6, this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH / 6 + 100, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6 - 5, this.mScreenH / 6, this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6 + 100, this.mScreenH / 6, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH / 6 + 100, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6 + 5, this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6 - 100, this.mScreenH / 6, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH - this.mScreenH / 6, this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH - this.mScreenH / 6 - 100, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6 - 5, this.mScreenH - this.mScreenH / 6, this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6 + 100, this.mScreenH - this.mScreenH / 6, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH - this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH - this.mScreenH / 6 - 100, this.mPaintLine);
            this.mCanvas.drawLine(this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6 + 5, this.mScreenH - this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6 - 100, this.mScreenH - this.mScreenH / 6, this.mPaintLine);
            this.mPaintText.setTextSize(this.tipTextSize);
            this.mPaintText.setAntiAlias(true);
            this.mPaintText.setDither(true);
            float f = this.mPaintText.measureText(this.tipText);
            this.mCanvas.drawText(this.tipText, this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6 + this.mScreenH / 2 - f / 2.0F, this.mScreenH / 6 - this.tipTextSize, this.mPaintText);
            Log.e("TAG", "left:" + (this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6));
            Log.e("TAG", "top:" + this.mScreenH / 6);
            Log.e("TAG", "right:" + (this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6));
            Log.e("TAG", "bottom:" + (this.mScreenH - this.mScreenH / 6));
        } catch (Exception localException) {
            localException.printStackTrace();
        } finally {
            if (this.mCanvas != null)
                this.mHolder.unlockCanvasAndPost(this.mCanvas);
        }
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //获得宽高，开启子线程绘图
        this.mScreenW = getWidth();
        this.mScreenH = getHeight();
        ractf = new RectF(this.mScreenW / 2 - this.mScreenH * 2 / 3 + this.mScreenH / 6, this.mScreenH / 6, this.mScreenW / 2 + this.mScreenH * 2 / 3 - this.mScreenH / 6, this.mScreenH - this.mScreenH / 6);
        this.mThread = new Thread(this);
        this.mThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //停止线程
        try {
            mThread.interrupt();
            mThread = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        //子线程绘图
        draw();
    }
}
